import router from './router'
import { useTitle } from '@/hooks/web/useTitle'
import { useNProgress } from '@/hooks/web/useNProgress'
import { usePageLoading } from '@/hooks/web/usePageLoading'
const whiteList = ['/login', '/findPwd', '/resetPwd'] // no redirect whitelist
import { token_get } from '@/utils/auth'
// import { useAppStoreWithOut } from '@/store/modules/app'
// import type { RouteRecordRaw } from 'vue-router'
// import { usePermissionStoreWithOut } from '@/store/modules/permission'
// import { NO_REDIRECT_WHITE_LIST } from '@/constants'
// import { useUserStoreWithOut } from '@/store/modules/user'
// import { getEncryptCode } from '@/api/common'

const { start, done } = useNProgress()

const { loadStart, loadDone } = usePageLoading()

router.beforeEach(async (to, from, next) => {
  start()
  loadStart()
  const hasToken = token_get()

  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' })
      done()
    } else {
      // if (store.getters.isPower) {
      //   if (to.meta && to.meta.chidden) {
      //     const rItem = store.getters.rights.find(right => right.Id === to.meta.id)
      //     if (rItem && rItem.OwnerId !== store.getters.module.id) {
      //       // recalc module rights
      //       await store.dispatch('account/loadModule', rItem.OwnerId)
      //       // reload routes
      //       await store.dispatch('permission/reloadRoutes')
      //     }
      //   }
      //   next()
      // } else {
      //   try {
      //     // get user info
      //     // note: rights must be a object array!
      //     const rights = await store.dispatch('account/getInfo')
      //     // generate accessible routes map based on roles
      //     await store.dispatch('permission/generateRoutes', rights)
      //     // hack method to ensure that addRoutes is complete
      //     // set the replace: true, so the navigation will not leave a history record
      //     next({ ...to, replace: true })
      //   } catch (error) {
      //     await store.dispatch('common/resetToken')
      //     next(`/login?redirect=${to.path}`)
      //     NProgress.done()
      //   }
      // }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      if (to.path === '/logout_bca9b21e9fe54915a656697b84b46821') {
        next('/login')
      } else {
        next(`/login?redirect=${to.path}`)
      }
      done()
    }
  }
  // console.log('1', getEncryptCode())
  // const permissionStore = usePermissionStoreWithOut()
  // const appStore = useAppStoreWithOut()
  // const userStore = useUserStoreWithOut()
  // if (userStore.getUserInfo) {
  //   if (to.path === '/login') {
  //     next({ path: '/' })
  //   } else {
  //     if (permissionStore.getIsAddRouters) {
  //       next()
  //       return
  //     }

  //     // 开发者可根据实际情况进行修改
  //     const roleRouters = userStore.getRoleRouters || []

  //     // 是否使用动态路由
  //     if (appStore.getDynamicRouter) {
  //       appStore.serverDynamicRouter
  //         ? await permissionStore.generateRoutes('server', roleRouters as AppCustomRouteRecordRaw[])
  //         : await permissionStore.generateRoutes('frontEnd', roleRouters as string[])
  //     } else {
  //       await permissionStore.generateRoutes('static')
  //     }

  //     permissionStore.getAddRouters.forEach((route) => {
  //       router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  //     })
  //     const redirectPath = from.query.redirect || to.path
  //     const redirect = decodeURIComponent(redirectPath as string)
  //     const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }
  //     permissionStore.setIsAddRouters(true)
  //     next(nextData)
  //   }
  // } else {
  //   if (NO_REDIRECT_WHITE_LIST.indexOf(to.path) !== -1) {
  //     next()
  //   } else {
  //     next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
  //   }
  // }
})

router.afterEach((to) => {
  useTitle(to?.meta?.title as string)
  done() // 结束Progress
  loadDone()
})
